package com.example.bikemanagerjava.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bikemanagerjava.common.Result;
import com.example.bikemanagerjava.entity.BikeOrder;
import com.example.bikemanagerjava.service.impl.BikeOrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin("*")
public class BikeOrderController {
    @Autowired
    private BikeOrderServiceImpl bikeOrderService;

    //创建订单
    @PostMapping("/orders")
    public Result addBikeOrder(@RequestBody BikeOrder bikeOrder) {

        bikeOrderService.save(bikeOrder); //插入一个单车订单

        return Result.success();
    }

    //获取订单详情
    @GetMapping("/orders/{id}")
    public Result getBike(@PathVariable Integer id) {

            BikeOrder bikeOrder = bikeOrderService.getById(id); //获取单个订单详情

            return Result.success(bikeOrder);
    }

    //更新订单
    @PutMapping("/orders")
    public Result putBikeOrder(@RequestBody BikeOrder bikeOrder) {

        bikeOrderService.updateById(bikeOrder);

        return Result.success();
    }

    //删除订单
    @DeleteMapping("/orders/{id}")
    public Result deleteBikeOrder(@PathVariable Integer id) {

        bikeOrderService.removeById(id);

        return Result.success();
    }

    //获取订单列表
    @GetMapping("/orders")
    public Result getAllBikeOrders(Integer pageNum,
                                  Integer pageSize,
                                   String userid) {

        QueryWrapper<BikeOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(userid),"userid",userid);
        Page<BikeOrder> page = bikeOrderService.page(new Page<>(pageNum, pageSize), queryWrapper);

        return Result.success(page);
    }

}
